<?php
declare (strict_types = 1);

namespace app\common\controller;

use app\BaseController;
use app\common\libs\CurlService;

/**
 * CURL请求示例
 *
 * @Author tanruizheng
 * @DateTime 2021-07-28
 */
class Request extends BaseController
{
    /**
     * get请求
     *
     * @Author tanruizheng
     * @DateTime 2021-07-28
     * @return void
     */
    public function getRequest(){
        $url = 'http://baidu.com';
        $result = CurlService::getRequest($url);
        dump($result);
    }

    /**
     * post请求
     *
     * @Author tanruizheng
     * @DateTime 2021-07-28
     * @return void
     */
    public function postRequest(){
        $url = url('common/Request/receiveData', [], true, true)->__toString();
        $data = [
            'token' => '12345678',
            'mobile' => [
                '13763017217',
                '13763017212'
            ]
        ];
        $result = CurlService::postRequest($url,$data);
        dump($result);
    }

    public function receiveData(){
        $token = request()->param('token');
        $mobile = request()->param('mobile/a');
        if(empty($token)){
            return json(['code' => '0', 'msg' => 'token不能为空!']);
        }
        if(empty($mobile)){
            return json(['code' => '0', 'msg' => 'mobile不能为空!']);
        }
        return json([
            'data' => [
                'token' => $token,
                'mobile' => implode(',',$mobile),
            ]
        ]);
    }
}
